package com.parking.mgr.common.util;

import cn.hutool.core.util.StrUtil;
import com.parking.mgr.common.constant.CommonConstant;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


/**
 * @ClassName TimstampUtil
 * @Description 时间戳工具类
 * @Auther: zhijianpu
 * @Date: 2023/5/25
 */
public class TimestampUtil {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");

    private static SimpleDateFormat monthFormat = new SimpleDateFormat("MM");

    private static SimpleDateFormat dayFormat = new SimpleDateFormat("dd");

    private static SimpleDateFormat weekFormat = new SimpleDateFormat("ww");

    private static SimpleDateFormat sdfWeek = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 生成时间戳
     * @return
     */
    public static long createTimestamp() {
        Instant timestamp = Instant.now(); // 获取当前时间戳
        return timestamp.toEpochMilli(); // 获取时间戳的毫秒数
    }

    /**
     * 生成token过期时间
     * @return
     */
    public static long extensionDays() {
        Date date = new Date();
        sdf.format(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, CommonConstant.EXTENSION_DAYS);
        return cal.getTime().getTime();
    }

    public static String timeStamp2CronDate(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("ss mm HH dd MM ? yyyy");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(time));
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String timestampToYear(long timestampMillis) {
        // 创建Date对象
        Date date = new Date(timestampMillis);
        // 创建Calendar对象，用于获取年、月、日、季、周
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 设置时区
        yearFormat.setTimeZone(TimeZone.getTimeZone("UTC+08:00"));
        // 使用format方法将时间戳转为字符串
        return yearFormat.format(date);
    }

    public static String timestampToMonth(long timestampMillis) {
        // 创建Date对象
        Date date = new Date(timestampMillis);
        // 创建Calendar对象，用于获取年、月、日、季、周
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 设置时区
        monthFormat.setTimeZone(TimeZone.getTimeZone("UTC+08:00"));
        // 使用format方法将时间戳转为字符串
        return monthFormat.format(date);
    }

    public static String timestampToDay(long timestampMillis) {
        // 转换为日期时间
        LocalDateTime dateTime = Instant.ofEpochMilli(timestampMillis)
                .atZone(ZoneId.systemDefault()) // 指定时区
                .toLocalDateTime();

        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd");
        return dateTime.format(formatter);
    }

    public static String timestampToWeek(long timestampMillis) {
        // 创建Date对象
        Date date = new Date(timestampMillis);
        // 创建Calendar对象，用于获取年、月、日、季、周
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 设置时区
        weekFormat.setTimeZone(TimeZone.getTimeZone("UTC+08:00"));
        // 使用format方法将时间戳转为字符串
        return weekFormat.format(date);
    }

    public static String timestampToQuarter(long timestampMillis) {
        // 创建Date对象
        Date date = new Date(timestampMillis);
        // 创建Calendar对象，用于获取年、月、日、季、周
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // 获取月份
        int month = calendar.get(Calendar.MONTH);

        // 计算季度
        int quarter = (month / 3) + 1;

        return String.valueOf(quarter);
    }

    public static long midnightTimestamp() {
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();

        // 设置时间为当天的00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 获取当天的00:00:00时刻
        Date midnight = calendar.getTime();

        // 获取时间戳（毫秒数）
        return midnight.getTime();
    }

    public static long lastSecondTimestamp() {
        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();

        // 设置时间为当天的23:59:59
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        // 获取当天的23:59:59时刻
        Date lastSecond = calendar.getTime();

        // 获取时间戳（毫秒数）
        return lastSecond.getTime();
    }

    public static String getQuar(String mm) {
        int month = Integer.parseInt(mm);
        int quarInt = (month + 2) / 3;
        return String.valueOf(quarInt);
    }

    public static String getWeek(String yyyy, String mm, String dd) {
        String weekStr = yyyy + "-" + mm + "-" + dd;
        String week = StrUtil.EMPTY;
        try {
            Date date = sdfWeek.parse(weekStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.setTime(date);
            week = String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        return week;
    }

    public static String getYyyyMMdd(String yyyy, String mm, String dd) {
        return yyyy  + mm + dd;
    }

    public static String getYyyyMM(String yyyy, String mm) {
        return yyyy + mm;
    }

    /**
     * 获取当天凌晨 12 点的时间戳
     * @return
     */
    public static long getTodayMidnightTimestamp() {
        // 获取当天日期
        LocalDate today = LocalDate.now();

        // 将当天日期设置为凌晨12点
        LocalDateTime midnight = today.atStartOfDay();

        // 将LocalDateTime转换为时间戳（毫秒）
        long timestamp = midnight.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

        return timestamp;
    }

    /**
     * 获取指定日期的 0 点时间戳
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static long getDayMidnightTimestamp(String year, String month, String day) {
        LocalDate specifiedDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); // 替换为目标日期

        return specifiedDate
                .atStartOfDay(ZoneId.systemDefault())
                .toInstant()
                .toEpochMilli();
    }

    /**
     * 要获取指定日期的 23点59分59秒 时间戳
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static long getDayLastSecondTimestamp(String year, String month, String day) {
        // 获取指定日期的 23:59:59 时间戳
        LocalDate specifiedDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); // 替换为目标日期
        return specifiedDate
                .atTime(LocalTime.of(23, 59, 59)) // 设置时间为 23:59:59
                .atZone(ZoneId.systemDefault()) // 使用系统默认时区
                .toInstant()
                .toEpochMilli();

    }

    public static void main(String[] args) {
        //System.out.println(getWeek("2024", "02", "22"));

        //System.out.println(BigDecimal.valueOf((double) 643089L / (double) 1713953573829L));

        //System.out.println(0 / 1000);

        //double noDataMinutes = ((double) 1709631489205L / 60000);

        //System.out.println(4564563 / noDataMinutes);

//        double duraTimeTmp = (30387D / 1000) * 60;
//
//        if (duraTimeTmp == CommonConstant.ZERO) {
//            System.out.println("0");
//        } else {
//            BigDecimal duraFreq = BigDecimal.valueOf(1795 / duraTimeTmp);
//            System.out.println(duraFreq);
//        }

        //System.out.println("指定日期0点的时间戳: " + getDayMidnightTimestamp("2024", "11", "21"));

        //System.out.println("指定日期 23:59:59 的时间戳: " +  getDayLastSecondTimestamp("2024", "11", "21"));
        System.out.println(timestampToDay(1732295646488L));
    }

}
